package teleradiologia.repository;

import org.hibernate.Session;
import org.hibernate.Transaction;

import teleradiologia.domain.Picture;
import teleradiologia.utils.HibernateUtil;

public class PictureDaoImpl implements PictureDao {

	@Override
	public long create(Picture picture) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		long id = (Long) session.save(picture);

		tx.commit();
		session.close();

		return ++id;
	}

	@Override
	public void delete(Picture picture) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		session.delete(picture);

		tx.commit();
		session.close();
	}

	@Override
	public Picture getById(long id) {
		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		Picture picture = (Picture) session.get(Picture.class, new Long(id));

		tx.commit();
		session.close();

		return picture;
	}
}
